package spar.cancer.config;

import spar.cancer.Cancer;
import spar.cancer.algorithm.invgen.InvGen;
import spar.cancer.algorithm.invgen.InvGenDaikon;
import spar.cancer.data.from.ecxt.Ecxt;
import spar.cancer.data.from.ecxt.EcxtCSV;
import spar.cancer.data.from.ecxt.EcxtRedis;
import spar.cancer.data.from.ecxt.EcxtSocket;
import spar.cancer.data.to.dump.Dump;
import spar.cancer.data.to.dump.DumpFile;
import spar.cancer.data.to.dump.DumpRedis;
import spar.cancer.data.to.dump.DumpStdout;
import spar.cancer.data.to.grptrace.GrpTrace;
import spar.cancer.data.to.grptrace.GrpTraceCSV;
import spar.cancer.data.to.grptrace.GrpTraceDaikon;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class CancerConfig {
    //app代码的文件名
    public String codeFile;
    //kmeans算法的k值
    public int kmeans;
    //dos阈值
    public double dosThro;
    //环境值数据来源
    public Ecxt ecxt;
    //check数据输出的去处
    public Dump dump;
    //group的trace输出的去处
    public GrpTrace grpTrace;
    //inv生成的算法
    public InvGen invGen;
    //dump flag
    public boolean dumpFlag;
    //grouping flag
    public boolean groupingFlag;
    //invGen flag
    public boolean invGenFlag;

    public CancerConfig() {

    }

    public CancerConfig(String configFile) throws IOException {
        List<String> lines = Files.readAllLines(Paths.get(configFile), StandardCharsets.UTF_8);
        for (String line : lines) {
            if (line.length() == 0 || line.charAt(0) == '#') {
                continue;
            } else {
                String[] strs = line.split(" ");
                switch (strs[0]) {
                    case "codeFile":
                        codeFile = strs[1];
                        break;
                    case "EcxtCSV":
                        ecxt = new EcxtCSV(strs[1]);
                        break;
                    case "EcxtRedis":
                        ecxt = new EcxtRedis(strs[1], Integer.parseInt(strs[2]));
                        break;
                    case "EcxtSocket":
                        ecxt = new EcxtSocket(Integer.parseInt(strs[1]));
                        break;
                    case "EcxtBind":
                        List<Cancer<Double>> objs = new ArrayList<>();
                        for (int i = 1; i < strs.length; i++) {
                            Cancer<?> obj = Cancer.get(strs[i]);
                            if (obj == null) {
                                obj = new Cancer<>(strs[i]);
                            }
                            objs.add((Cancer<Double>) obj);
                        }
                        ecxt.bind(objs);
                        break;
                    case "dump":
                        dumpFlag = Boolean.parseBoolean(strs[1]);
                        break;
                    case "DumpStdout":
                        dump = new DumpStdout();
                        break;
                    case "DumpRedis":
                        dump = new DumpRedis(strs[1], Integer.parseInt(strs[2]));
                        break;
                    case "DumpFile":
                        dump = new DumpFile(strs[1]);
                        break;
                    case "grouping":
                        groupingFlag = Boolean.parseBoolean(strs[1]);
                        break;
                    case "kmeans":
                        kmeans = Integer.parseInt(strs[1]);
                        break;
                    case "dosThro":
                        dosThro = Double.parseDouble(strs[1]);
                        break;
                    case "GrpTraceDaikon":
                        grpTrace = new GrpTraceDaikon(strs[1]);
                        break;
                    case "GrpTraceCSV":
                        grpTrace = new GrpTraceCSV(strs[1]);
                        break;
                    case "invGen":
                        invGenFlag = Boolean.parseBoolean(strs[1]);
                        break;
                    case "InvGenDaikon":
                        invGen = new InvGenDaikon(strs[1], strs[2]);
                        break;
                    default:
                        System.err.println("Unknown config:" + line);
                        break;
                }
            }
        }
    }
}
